# 本代码主要参考：https://www.imooc.com/video/15035
# 本节主要讲的是 人工神经网络

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD

from sklearn.datasets import load_iris
from sklearn.preprocessing import LabelBinarizer
from sklearn.cross_validation import train_test_split

iris = load_iris()

LabelBinarizer().fit_transform(iris["target"])

# print(iris["target"])

# print(LabelBinarizer().fit_transform(iris["target"]))

train_data, test_data, train_target, test_target = train_test_split(iris.data, iris.target, test_size=0.2, random_state=1)

labels_train = LabelBinarizer().fit_transform(train_target)
labels_test = LabelBinarizer().fit_transform(test_target)

model = Sequential(
    [
        Dense(5, input_dim=4),
        Activation("relu"),
        Dense(3),
        Activation("sigmoid"),
    ]
)

# model = Sequential()
# model.add(Dense(5, input=4))

sgd = SGD(lr=0.01, decay=1e-6,momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss="categorical_crossentropy")
model.fit(train_data, labels_train, nb_epoch=200, batch_size=40)
print(model.predict_classes(test_data))


# model.sample_weight_modes("./data/sklear_03")
model.save_weights("./data/sklear_03")
# model.load_weights("./data/sklear_03")